<template>
  <div class="cs-page-container">
    <div class="cs-filter-container">
      <el-button type="primary" size="small" @click="addAnchorWeight()">主播权重</el-button>
    </div>
    <div class="cs-filter-result">
      <el-table
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column label="影响参数" align="center" width="180"> <!-- ======================================== -->
          <template slot-scope="scope">
            <span>{{ params[scope.row.type] }}</span>
          </template>
        </el-table-column>

        <el-table-column label="MAX" align="center">  <!-- ======================================== -->
          <template slot-scope="scope">
            <span>{{ scope.row.numMax }}</span>
          </template>
        </el-table-column>

        <el-table-column label="MIN" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.numMin }}</span>
          </template>
        </el-table-column>

        <el-table-column label="加权" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.weightRate }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="操作"
          width="180"
        >
          <template slot-scope="{row}">
            <el-button
              type="primary"
              size="mini"
              @click="showDialog(row)"
            >修改</el-button>
          </template>
        </el-table-column>

      </el-table>

      <el-dialog
        v-loading="dialogLoading"
        :title="title"
        :visible.sync="dialogVisible"
        width="50%"
      >
        <el-form ref="updateOb" :model="updateOb" label-width="80px">
          <el-form-item label="MAX：" style="display: table">
            <el-input v-model="updateOb.numMax" placeholder="" />
          </el-form-item>
          <el-form-item label="MIN：" style="display: table">
            <el-input v-model="updateOb.numMin" placeholder="" />
          </el-form-item>
          <el-form-item label="加权：" style="display: table">
            <el-input v-model="updateOb.weightRate" placeholder="" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="danger" @click="updateWeight()">确 定</el-button>
        </span>
      </el-dialog>

    </div>
    <br>
    <div class="cs-filter-container">
      <span>基础权重: </span>
      <el-input v-model="basicWeight" clearable placeholder="" />
      <el-button type="primary" @click="updateBasicWeight()">修改</el-button>
    </div>
  </div>
</template>

<script>
import { xhrWeightUpdate, xhrWeightList } from '@/api/customSetting'
export default {
  name: 'WeightSetting',
  data() {
    return {
      list: null,
      dialogLoading: true,
      dialogVisible: false,
      listLoading: true,
      updateOb: {
        basicWeight: null,
        id: null,
        numMax: null,
        numMin: null,
        weightRate: null
      },
      basicWeight: null,
      title: null,
      params: [
        '在线接通率',
        '被拨打次数',
        '在线时长',
        '盈利积分',
        '主播信用分'
      ]
    }
  },
  created() {
    this.doSearch()
  },
  methods: {
    doSearch() {
      xhrWeightList()
        .then(resp => {
          this.list = resp.data.userWeightConfigVoList
          // this.updateOb.basicWeight = resp.data.basicWeight
          this.basicWeight = resp.data.basicWeight
          this.listLoading = false
        })
        .catch(err => {
          this.$message.error(err)
          this.listLoading = false
        })
    },
    showDialog(row) {
      this.updateOb.id = row.id
      this.title = this.params[row.type]
      this.dialogVisible = true
      this.dialogLoading = false
    },
    updateBasicWeight() {
      this.updateOb = {
        basicWeight: this.basicWeight,
        id: 6,
        numMax: 0,
        numMin: 0,
        weightRate: 0
      }
      if (this.updateOb.basicWeight && !/^\d*$/.test(this.updateOb.basicWeight)) {
        this.$message.warning('配置只能是数字')
        this.listLoading = false
        this.dialogLoading = false
        this.updateOb.basicWeight = this.basicWeight
        return
      }
      if (!(this.updateOb.basicWeight)) {
        this.$message.warning('配置不能为空')
        this.listLoading = false
        this.dialogLoading = false
        this.updateOb.basicWeight = this.basicWeight
        return
      }
      this.updateWeight()
    },
    updateWeight() {
      this.dialogLoading = true
      if ((this.updateOb.numMax && !/^\d*$/.test(this.updateOb.numMax)) || (this.updateOb.numMin && !/^\d*$/.test(this.updateOb.numMin)) || (this.updateOb.weightRate && !/^\d*$/.test(this.updateOb.weightRate))) {
        this.$message.warning('配置只能是数字')
        this.listLoading = false
        this.dialogLoading = false
        this.updateOb.numMax = null
        this.updateOb.numMin = null
        this.updateOb.weightRate = null
        return
      }
      if ((this.updateOb.numMax && this.updateOb.numMin) && (parseInt(this.updateOb.numMax) < parseInt(this.updateOb.numMin))) {
        this.$message.warning('配置MAX必须大于MIN')
        this.listLoading = false
        this.dialogLoading = false
        this.updateOb.numMax = null
        this.updateOb.numMin = null
        this.updateOb.weightRate = null
        return
      }
      if (this.updateOb.numMax && !this.updateOb.numMin) {
        this.list.forEach(item => {
          if (item.id === this.updateOb.id && parseInt(this.updateOb.numMax) < parseInt(item.numMin)) {
            this.$message.warning('配置MAX必须大于初始MIN')
            this.listLoading = false
            this.dialogLoading = false
            this.updateOb.numMax = null
            this.updateOb.numMin = null
            this.updateOb.weightRate = null
            this.flag = true
            return false
          }
        })
      } else if (this.updateOb.numMin && !this.updateOb.numMax) {
        this.list.forEach(item => {
          if (item.id === this.updateOb.id && parseInt(this.updateOb.numMin) > parseInt(item.numMax)) {
            this.$message.warning('配置MIN必须小于MAX')
            this.listLoading = false
            this.dialogLoading = false
            this.updateOb.numMax = null
            this.updateOb.numMin = null
            this.updateOb.weightRate = null
            this.flag = true
            return false
          }
        })
      }
      if (this.flag) {
        return
      }

      this.weightRate = null
      xhrWeightUpdate(this.updateOb)
        .then(resp => {
          this.dialogVisible = false
          this.dialogLoading = false
          this.resetUpdateOb()
          this.showResult()
        })
        .catch(err => {
          this.$message.error(err)
          // this.listLoading = false
          this.resetUpdateOb()
          this.dialogLoading = false
        })
    },
    resetUpdateOb() {
      this.updateOb = {
        basicWeight: this.basicWeight,
        id: this.updateOb.id,
        numMax: null,
        numMin: null,
        weightRate: null
      }
    },
    showResult() {
      this.doSearch()
      this.$message.success('操作成功!')
    },
    addAnchorWeight() {
      this.$router.push('/app-settings/anchor-weight')
    }
  }
}
</script>
